| Platform | Build Status |
|---|---|
| Linux | |
| macOS | |
| Windows |
Features
- Async and sync actors
- Actor communication in a local/thread context
- Uses futures for asynchronous message handling
- Actor supervision
- Typed messages (No
Anytype) - Runs on stable Rust 1.40+
Usage
To use actix, add this to your Cargo.toml:
[]
= "0.10"
Initialize Actix
In order to use actix you first need to create a System.
Actix uses the tokio event loop.
System::new() creates a new event loop and starts the System actor.
system.run() starts the tokio event loop, and will finish once the System actor
receives the SystemExit message.
Let's create a simple Actor.
Implement an Actor
In order to define an actor you need to define a struct and have it implement
the Actor trait.
use ;
;
Spawning a new actor is achieved via the start and create methods of
the Actor
trait. It provides several different ways of creating actors, for details check docs.
You can implement started, stopping and stopped methods of the Actor trait.
started gets called when actor starts and stopping when actor finishes.
Check API documentation
for more information on the actor lifecycle.
Handle Messages
An Actor communicates with another Actor by sending messages. In actix all messages
are typed. Let's define a simple Sum message with two usize parameters,
and an actor which will accept this message and return the sum of those two numbers.
Here we use the actix-rt as way start our System
and drive our main Future so we can easily .await for the messages sent to the Actor.
use *;
// this is our Message
// we have to define the response type for `Sum` message
;
// Actor definition
;
// now we need to define `MessageHandler` for the `Sum` message.
// <- starts the system and block until future resolves
async
All communications with actors go through an Addr object. You can do_send a message
without waiting for a response, or send an actor with specific message. The Message
trait defines the result type for a message.
Actor State And Subscription For Specific Messages
You may have noticed that methods of Actor and Handler traits accept &mut self, so you are
welcome to store anything in an actor and mutate it whenever necessary.
Address objects require an actor type, but if we just want to send a specific message to
an actor that can handle the message, we can use the Recipient interface. Let's create
a new actor that uses Recipient.
use *;
use Duration;
// Actor definition
// simple message handler for Ping message
Chat Example
There is a chat example which provides a basic example of networking client/server service.
Contributing
All contributions are welcome, if you have a feature request don't hesitate to open an issue!
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Code of Conduct
Contribution to the actix crate is organized under the terms of the Contributor Covenant, the maintainers of actix, promises to intervene to uphold that code of conduct.